<?php

namespace app\parent\service;

use app\common\service\BaseService;
use app\lib\exception\BaseException;
use app\models\ClassStudentWorkModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\GradesDetailsListsModel;
use app\models\GradesDetailsModel;
use app\models\ParentInformationModel;
use app\models\StudentModel;
use app\models\StudentSubjectClassModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\StudentWorkModel;
use app\models\UserModel;
use app\models\WorkCorrectionModel;
use app\models\WorkModel;
use think\Db;

class TaskService extends BaseService
{
    /**
     * 作业列表
     * User: wufeng
     * Date: 2020/03/01.
     *
     * @return array
     */
    public function taskLists()
    {
        try {
            $where = [];
            $student_id = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
            $campus_id = (new StudentModel())->get($student_id)['campus_id'];
            $cmdl_ids = (new StudentSubjectInfosClassModel())->where(['campus_id' => $campus_id, 'student_id' => $student_id])->column('class_manage_details_id');
            $where['cmdl_id'] = ['in', implode(',', $cmdl_ids)];
            $this->data['lists'] = (new WorkModel())
                ->where($where)
                ->select()
                ->toArray();
            foreach ($this->data['lists'] as &$datum) {
                $ids = (new GradesDetailsListsModel())->order('class_date asc')->column('id');
                $i = array_search($datum['cmdl_id'], $ids, true);
                $j = $i + 1;
                $datum['class_day'] = '第'.$j.'节';
                $gradesDetailsLists = (new GradesDetailsListsModel())->get($datum['cmdl_id']);
                $datum['course_name'] = (new CourseModel())->get($gradesDetailsLists['course_id'])['course_name'];
                $datum['headmaster'] = $gradesDetailsLists['headmaster'];
                $datum['teacher'] = $gradesDetailsLists['teacher'];
                $datum['assistant'] = $gradesDetailsLists['assistant'];
                $datum['picture'] = explode(',', $datum['picture']);
            }
            $this->data['count'] = (new WorkModel())
                ->where($where)
                ->count();
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, '');
        }

        return $this->return;
    }

    /**
     * 作业班级列表
     * User: wufeng
     * Date: 2020/03/11.
     *
     * @param $params
     *
     * @return array
     */
    public function taskClassLists($params)
    {
        try {
            $where = [];
            $student_id = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
            $campus_id = (new StudentModel())->get($student_id)['campus_id'];
            $class_subject_ids = (new StudentSubjectInfosClassModel())->where(['campus_id' => $campus_id, 'student_id' => $student_id])->column('class_subject_id');
            $where['id'] = ['in', implode(',', array_unique($class_subject_ids))];
            $where['graduation_status'] = 0;
            $this->data = (new GradesDetailsModel())
                ->where($where)
                ->select()
                ->toArray();
            foreach ($this->data as &$datum) {
                $ids = (new GradesDetailsListsModel())->where(['class_manage_id' => $datum['id']])->column('id');
                $datum['release_times'] = (new WorkModel())->where(['cmdl_id' => ['in', implode(',', $ids)]])->count();
                $datum['course_type'] = (new CourseModel())->get(['id' => $datum['course_id']])['course_type'];
                $datum['course_type_text'] = (new DictionaryModel())->get(['id' => $datum['course_type']])['name'];
                $datum['room'] = (implode('|', array_unique(explode(',', $datum['room_text']))));
                $arrs = (new StudentSubjectClassModel())->where('class_subject_id', $datum['id'])->column('student_id');
                $datum['already_reported_student_num'] = \count(array_unique($arrs));
            }
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * 作业详情列表
     * User: wufeng
     * Date: 2020/03/11.
     *
     * @param $params
     *
     * @return array
     */
    public function taskDetailLists($params)
    {
        try {
            $where = [];
            $ids = (new GradesDetailsListsModel())->where(['class_manage_id' => $params['id']])->column('id');
            $where['cmdl_id'] = ['in', implode(',', $ids)];
            $this->data = (new WorkModel())
                ->where($where)
                ->order('id desc')
                ->select()
                ->toArray();
            foreach ($this->data as &$datum) {
                $datum['name'] = (new UserModel())->get($datum['user_id'])['name'];
                $classStudentWork = (new ClassStudentWorkModel())->get(['work_id' => $datum['id'], 'cmdl_id' => $datum['cmdl_id']]);
                $datum['is_submission'] = $classStudentWork['is_submission'];
                $datum['is_correction'] = $classStudentWork['is_correction'];
            }
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/03.
     *
     * @param $params
     *
     * @return array
     */
    public function taskDetail($params)
    {
        try {
            $this->data['work_detail'] = (new WorkModel())->get($params['id']);
            if ($this->data['work_detail']) {
                $this->data['work_detail']['publisher'] = (new UserModel())->get($this->data['work_detail']['user_id'])['name'];
                if ('' != $this->data['work_detail']['picture'] || null != $this->data['work_detail']['picture']) {
                    $this->data['work_detail']['picture'] = explode(',', $this->data['work_detail']['picture']);
                } else {
                    $this->data['work_detail']['picture'] = [];
                }
            }
            $this->data['work_correction_detail'] = (new WorkCorrectionModel())->get(['work_id' => $params['id'], 'cmdl_id' => $params['cmdl_id']]);
            if ($this->data['work_correction_detail']) {
                $this->data['work_correction_detail']['publisher'] = (new UserModel())->get($this->data['work_correction_detail']['user_id'])['name'];
                if ('' != $this->data['work_correction_detail']['picture'] || null != $this->data['work_correction_detail']['picture']) {
                    $this->data['work_correction_detail']['picture'] = explode(',', $this->data['work_correction_detail']['picture']);
                } else {
                    $this->data['work_correction_detail']['picture'] = [];
                }
            }

            $classStudentWork = (new ClassStudentWorkModel())->get(['work_id' => $params['id'], 'cmdl_id' => $params['cmdl_id']]);
            if (1 === (int) $classStudentWork['is_submission']) {
                $student_id = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
                $this->data['student_work'] = (new StudentWorkModel())->get(['work_id' => $params['id'], 'cmdl_id' => $params['cmdl_id'], 'student_id' => $student_id]);
                if ('' != $this->data['student_work']['picture'] || null != $this->data['student_work']['picture']) {
                    $this->data['student_work']['picture'] = explode(',', $this->data['student_work']['picture']);
                } else {
                    $this->data['student_work']['picture'] = [];
                }
            } else {
                $this->data['student_work'] = null;
            }
            if (empty($this->data)) {
                throw new BaseException('NO_DATA');
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * 发布作业
     * User: wufeng
     * Date: 2020/03/01.
     *
     * @param $params
     *
     * @return array
     */
    public function submissionWork($params)
    {
        try {
            $data = [];
            $student_id = (new ParentInformationModel())->get(parent_get_current_user_id())['student_id'];
            $data['cmdl_id'] = $params['cmdl_id']; //课节表ID
            $data['work_id'] = $params['id']; //作业id
            $data['datetime'] = date('Y-m-d H:i:s'); //作业时间
            $data['student_id'] = $student_id;
            $data['content'] = $params['content']; //具体内容
            $data['audio'] = $params['audio']; //音频
            $data['audio_duration'] = $params['audio_duration']; //音频时长
            $data['video'] = $params['video']; //视频
            $data['picture'] = $params['picture']; //图片
            Db::name('edu_student_work')->insert($data);
            $class_student_work = [];
            $class_student_work['work_id'] = $params['id'];
            $class_student_work['cmdl_id'] = $params['cmdl_id'];
            $class_student_work['cssi_id'] = (new StudentSubjectInfosClassModel())->get(['class_manage_details_id' => $params['cmdl_id'],'student_id' => $student_id])['id'];
            Db::name('edu_class_student_work')->where($class_student_work)->update(['is_submission' => 1]);
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }
}
